package models

import (
	"database/sql"
)

type READER struct {
	// 假设你的数据结构有这些字段，根据实际情况调整
	RDID        string // 读者ID
	RDPASSWD    string // 读者密码
	RDNAME      string // 读者姓名
	RDCERTIFY   string // 身份证号
	RDSEX       string // 读者性别 0女1男
	RDCFSTATE   string // 读者状态 读者证状态，有效=1、验证=2、丢失=3、暂停=4、注销=5
	RDTYPE      string // 读者类型
	RDLIB       string //开户馆  SYZX
	RDSTARTDATE string //读者证有效启用日期
	RDENDDATE   string // 读者证有效期终止日期
	RDUNIT      string // 单位
	RDINTIME    string // 读者办证时间
	DEPARTMANT  string // 部门
	WORKCARDNO  string // 读者ID
	CLASS_NUM   string // 班级
}

func ReadIn(db *sql.DB, data *READER) error {

	// 检查数据是否存在于表中
	queryExist := "SELECT COUNT(*) FROM READER WHERE RDID = :1"
	var count int
	err := db.QueryRow(queryExist, data.RDID).Scan(&count)
	if err != nil {
		lg.Errorf("Error checking data existence: %v", err)
		return err
	}
	lg.Debug("ReadIn count ", count)
	if count == 0 {
		// 数据不存在，插入数据
		queryInsert := `INSERT INTO READER (RDID, RDPASSWD, RDNAME, RDCERTIFY, RDSEX, RDCFSTATE, RDTYPE, RDLIB, RDSTARTDATE, RDENDDATE, RDUNIT, RDINTIME, DEPARTMANT, WORKCARDNO, CLASS_NUM) VALUES (:1, :2, :3, :4, :5, :6, :7, :8, TO_DATE(:9, 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE(:10, 'SYYYY-MM-DD HH24:MI:SS'), :11, TO_DATE(:12, 'SYYYY-MM-DD HH24:MI:SS'), :13, :14, :15)`
		smt, err := db.Prepare(queryInsert)
		if err != nil {
			return err
		}
		defer smt.Close()
		_, err = smt.Exec(data.RDID, data.RDPASSWD, data.RDNAME, data.RDCERTIFY, data.RDSEX, data.RDCFSTATE, data.RDTYPE, data.RDLIB, data.RDSTARTDATE, data.RDENDDATE, data.RDUNIT, data.RDINTIME, data.DEPARTMANT, data.WORKCARDNO, data.CLASS_NUM)
		if err != nil {
			return err
		}
		lg.Debug("ReadIn Inserted new data", data.RDID)
	} else {
		// // 数据存在，检查是否需要更新
		// queryCheck := "SELECT RDID, RDNAME, RDCERTIFY, RDSEX, RDCFSTATE, RDTYPE, RDLIB, RDSTARTDATE, RDENDDATE, RDINTIME, WORKCARDNO, CLASS_NUM FROM READER WHERE RDID = :1"
		// var existingData READER
		// err = db.QueryRow(queryCheck, data.RDID).Scan(&existingData.RDID, &existingData.RDNAME, &existingData.RDCERTIFY, &existingData.RDSEX, &existingData.RDCFSTATE, &existingData.RDTYPE, &existingData.RDLIB, &existingData.RDSTARTDATE, &existingData.RDENDDATE, &existingData.RDINTIME, &existingData.WORKCARDNO, &existingData.CLASS_NUM)
		// if err != nil {
		// 	return err
		// }
		// equal := reflect.DeepEqual(existingData, data)
		// lg.Debugf("ReadIn equal %v", equal)
		// if !equal {
		// 数据不同，更新数据
		queryUpdate := `UPDATE READER SET RDPASSWD = :1, RDNAME = :2, RDCERTIFY = :3 , RDSEX = :4, RDCFSTATE = :5, RDTYPE = :6, RDLIB = :7, RDSTARTDATE = TO_DATE(:8, 'SYYYY-MM-DD HH24:MI:SS'), RDENDDATE = TO_DATE(:9, 'SYYYY-MM-DD HH24:MI:SS'), RDUNIT = :10, RDINTIME = TO_DATE(:11, 'SYYYY-MM-DD HH24:MI:SS'), DEPARTMANT = :12, WORKCARDNO = :13, CLASS_NUM = :14 WHERE RDID = :15`
		smt, err := db.Prepare(queryUpdate)
		if err != nil {
			return err
		}
		defer smt.Close()
		_, err = smt.Exec(data.RDPASSWD, data.RDNAME, data.RDCERTIFY, data.RDSEX, data.RDCFSTATE, data.RDTYPE, data.RDLIB, data.RDSTARTDATE, data.RDENDDATE, data.RDUNIT, data.RDINTIME, data.DEPARTMANT, data.WORKCARDNO, data.CLASS_NUM, data.RDID)
		if err != nil {
			return err
		}
		lg.Debug("ReadIn Updated existing data", data.RDID)
		// }
	}
	return nil
}
